package com.zsguan.z.meixi;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.zsguan.s.stools4andr.base.BaseActivity;
import com.zsguan.z.zhttp4andr.ZHttpUrlConnection;

/**
 * Created by chshqiang on 15-9-4.
 * http和https请求
 */
public class ZHttpActivity extends BaseActivity {
    private static final int HTTP = 1;
    private static final int HTTPS = 2;

    private static final int GET = 1;
    private static final int POST = 2;

    private RadioGroup httpGroup, httpMethodGroup;
    private RadioButton httpPostRadio;

    private int http, method;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);

        httpGroup = (RadioGroup) findViewById(R.id.httpGroup);
        httpMethodGroup = (RadioGroup) findViewById(R.id.httpMethodGroup);

        httpPostRadio = (RadioButton) findViewById(R.id.httpPostRadio);

        http = HTTP;
        method = GET;

        setup();
    }

    private void setup() {
        httpGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.httpRadio:
                        selectHttpRadio();
                        break;
                    case R.id.httpsRadio:
                    default:
                        selectHttpsRadio();
                        break;
                }
            }
        });

        httpMethodGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.httpGetRadio:
                        selectHttpGetRadio();
                        break;
                    case R.id.httpPostRadio:
                        selectHttpPostRadio();
                    default:
                        break;
                }
            }
        });
    }

    private void selectHttpPostRadio() {
        method = POST;
    }

    private void selectHttpGetRadio() {
        method = GET;
    }

    private void selectHttpsRadio() {
        http = HTTPS;

        httpPostRadio.setChecked(true);
        selectHttpPostRadio();
    }

    private void selectHttpRadio() {
        http = HTTP;

        httpMethodGroup.setEnabled(true);
    }

    public void onSummit(View view) {
        if (HTTP == http && GET == method) {
            summitGetHttp();
        } else if (HTTP == http && POST == method) {
            summitPostHttp();
        } else if (HTTPS == http && GET == method) {
            summitGetHttps();
        } else /* if (HTTPS == http && POST == method) */ {
            summitPostHttps();
        }
    }

    private void summitGetHttp() {
    }

    private void summitPostHttp() {

    }

    private void summitGetHttps() {

    }

    private void summitPostHttps() {
        try {
            ZHttpUrlConnection.initHttpsURLConnection(ZHttpActivity.this, "bestvwin", "", "server.cer");
            ZHttpUrlConnection.post("", "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
